const {ccclass, property} = cc._decorator;
import { EVT_CREATE_BULLET, EVT_PLAYER_HIT } from "../Core/AppEvent";
import Bullet from "../Bullet";
import { addListener, dispatchEvent } from "./EventManager";
import { WEAPON_TYPE, PLAYER_TYPE } from "../Core/AppDef";
import WindowUtils from "../Tools/WindowUtils";


@ccclass
export default class BulletManager extends cc.Component {s
    @property(cc.Prefab)
    bulletPrefab: cc.Prefab = null;
    @property(cc.Node)
    bulletLayer: cc.Node = null;

    bulletList : cc.Node[][] = [[],[]];
    collectBulletPool : cc.NodePool[] = [new cc.NodePool(WEAPON_TYPE.My+''),new cc.NodePool(WEAPON_TYPE.Enumy+'')];

    maxNum = 10;

    onLoad () {
        addListener(EVT_CREATE_BULLET, this.createBullet, this);

        this.onScheduleMove();
        this.init();
    }


    init () {
    }
    
    onScheduleMove () {
        this.unschedule(()=>{
            this.onCheckOutsideWindow();
        })
        this.schedule(()=>{
            this.onCheckOutsideWindow();
        }, 0.02)
    }

    onCheckOutsideWindow () {
        for(let j=0; j<2; j++){
            for(let i=0; i<this.bulletList[j].length; i++){
                let offset = 50;
                let pos = this.bulletList[j][i].position;
                if(pos.x > (WindowUtils.DEVICE_W + offset) || pos.x < -offset
                || pos.y > (WindowUtils.DEVICE_H + offset) || pos.y < -offset){
                    this.addCollectList(this.bulletList[j][i], i);
                }
            }
        }
    }

    addCollectList (node, index) {
        cc.log("_-- 回收池插入 addCollectList node.name  ", node.name);
        let script = node.getComponent(Bullet);
        script.onUnScheduleMove();
        
        if(this.collectBulletPool[script.bulletType].size() >= this.maxNum){
            node.destroy();
        }else{
            this.collectBulletPool[script.bulletType].put(node);
            if(index){
                this.bulletList[script.bulletType].splice(index, 1);
            }else{
                for(let j=0; j<2; j++){
                    for(let i=0; i<this.bulletList[j].length; i++){
                        if(node.name == this.bulletList[j][i].name){
                            this.bulletList[script.bulletType].splice(index, 1);
                        }
                    }
                }
            }
        }

    }

    checkCollectList(weaponType : WEAPON_TYPE){
        let length = this.collectBulletPool[weaponType].size();
        if(length > 0){
            cc.log("_-- 回收池创建 createBullet weaponType ", weaponType);
            let bullet = this.collectBulletPool[weaponType].get();
            bullet.parent = this.bulletLayer;
            this.bulletList[weaponType].push(bullet);
            bullet.name = "bullet-weapon" + weaponType + '-index' + (this.bulletList[weaponType].length - 1);
            let script = bullet.getComponent(Bullet);
            script.onReset();
            return bullet
        }else{
            cc.log("_-- 新建 createBullet weaponType ", weaponType);
            let bullet = cc.instantiate(this.bulletPrefab);
            this.bulletList[weaponType].push(bullet);
            bullet.name = "bullet-weapon" + weaponType + '-index' + (this.bulletList[weaponType].length - 1);
            this.bulletLayer.addChild(bullet);
            return bullet;
        }
    }

    createBullet (event: cc.Event.EventCustom) {
        let data = event.getUserData();
        // cc.log("_-- createBullet data ", data);

        let node = this.checkCollectList(data.weaponType); 
        node.position = data.pos;
        let script = node.getComponent(Bullet);
        script.setP(data.weaponP);
        script.setBulletType(data.weaponType, data.from);

        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);
        node.position = node.position.add(data.weaponP);

        let self = this;
        script.onBeginContact = function(contact, selfCollider, otherCollider){
            cc.log('onBeginContact otherCollider ' , otherCollider);
            var nameList = otherCollider.node.name.split("playerType=");
            cc.log('onBeginContact PLAYER_TYPE ' , nameList[1]);
            if(nameList[1] == PLAYER_TYPE.Enumy){
                self.addCollectList(node, null);
                dispatchEvent(EVT_PLAYER_HIT, {playerName:otherCollider.node.name} )
            }
        }

    }

    public Destroy() {
        this.collectBulletPool[0].clear();
        this.collectBulletPool[1].clear();
    }



}
